package com.app.backend.password.check.aop;

import com.app.backend.password.check.config.PasswordCheckProperties;
import com.app.backend.password.check.constant.AspectSequenceConstant;
import com.app.backend.password.check.service.DecryptService;
import com.app.backend.password.check.service.DecryptServiceContext;
import com.app.backend.password.check.vo.DecryptParamBean;
import com.app.backend.password.check.vo.RequestBean;
import com.app.backend.common.exception.AppException;
import com.app.backend.common.utils.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 用户帐号检查切面
 *
 * @author Jet Zheng
 */
@Aspect
@Component
@Slf4j
@Order(AspectSequenceConstant.CHECK_FIRST)
public class CheckPasswordAspect {

    @Autowired
    private DecryptServiceContext decryptServiceContext;

    @Autowired
    private PasswordCheckProperties passwordCheckProperties;

    private final static String PW_PATTERN = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,}$";

    @Around("@annotation(com.app.backend.password.check.aop.CheckPassword)")
    public Object doAroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        RequestBean bean = RequestBean.get();
        if (!ObjectUtil.checkObjFieldValueIsNull(bean)) {
            String password = bean.getPassword();
            DecryptService service = decryptServiceContext.getCardInstance(passwordCheckProperties.getDecryptType());
            DecryptParamBean decryptParamBean = DecryptParamBean.builder()
                    .aesKey(passwordCheckProperties.getKey())
                    .ivParameter(passwordCheckProperties.getIv())
                    .content(password)
                    .build();
            service.setParams(decryptParamBean);
            String passwordDecrypted = service.doDecrypt(decryptParamBean);
            if (!passwordDecrypted.matches(PW_PATTERN)) {
                throw new AppException("密码校验失败！");
            }
        } else {
            throw new AppException("请输入相关密码!");
        }
        Object obj = joinPoint.proceed();
        return obj;
    }

    public static void main(String[] args) {
        String pw1 = "ABCDEFGHIG";
        String pw2 = "abcdefghig";
        String pw3 = "0123456789";
        String pw4 = "!@#$%^&*()";
        String pw5 = "ABCDEabcde";
        String pw6 = "ABCDE01234";
        String pw7 = "ABCDE!@#$%";
        String pw8 = "abcde01234";
        String pw9 = "abcde!@#$%";
        String pw10 = "01234!@#$%";
        String pw11 = "abcde01234!@#$%";
        String pw12 = "ABCDE01234!@#$%";
        String pw13 = "ABCDEabcde!@#$%";
        String pw14 = "ABCDEabcde01234";
        String pw15 = "Aa0!";
        //符合要求密码
            String pw16 = "ABCabc012!@#";
        System.out.println(pw4.matches(PW_PATTERN));
        System.out.println(pw5.matches(PW_PATTERN));
        System.out.println(pw6.matches(PW_PATTERN));
        System.out.println(pw7.matches(PW_PATTERN));
        System.out.println(pw8.matches(PW_PATTERN));
        System.out.println(pw9.matches(PW_PATTERN));
        System.out.println(pw10.matches(PW_PATTERN));
        System.out.println(pw11.matches(PW_PATTERN));
        System.out.println(pw12.matches(PW_PATTERN));
        System.out.println(pw13.matches(PW_PATTERN));
        System.out.println(pw14.matches(PW_PATTERN));
        System.out.println(pw15.matches(PW_PATTERN));
        System.out.println(pw16.matches(PW_PATTERN));
    }
}
